package org.shop.controller.supplier;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Supplier;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.SupplierService;
import org.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("supplier")
public class SupplierController {
	
	private static Logger logger = LogManager.getLogger(SupplierController.class);
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private UserService userService;
	
	/**
     * 添加供应商
     * @param req
     * @return
     */
    @RequestMapping("addSupplier")
    @ResponseBody
    public Map<String, Object> addSupplier(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String supplierName = req.getParameter("supplierName");
        String linkName = req.getParameter("linkName");
        String linkPhone = req.getParameter("linkPhone");
        String address = req.getParameter("address");
        //获取登录人用户信息
        MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Map<String, Object> paramMap=new HashMap<String, Object>();
        	paramMap.put("supplierName", supplierName);
        	Supplier supplier=supplierService.findSupplierBySupplierName(paramMap);
        	if(supplier!=null){
        		map.put("state", 0);
                map.put("msg", "供货商名称已经存在！");
                return map;
        	}
        	supplier=new Supplier(); 
        	supplier.setUserId(ud.getUserId());
        	supplier.setSupplierName(supplierName);
        	supplier.setLinkName(linkName);
        	supplier.setLinkPhone(linkPhone);
        	supplier.setAddress(address);
        	supplier.setCreateDate(new Date());
        	supplier.setState((byte)1);        	
            supplierService.insertSelective(supplier);
            Users users=new Users();
            users.setUserId(ud.getUserId());
            users.setUserName(linkName);
            users.setRealName(linkName);
            userService.updateByPrimaryKeySelective(users);
            map.put("state", 1);
            map.put("msg", "提交成功");
        } catch (Exception e) {
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "供货商开通失败");
        }
        return map;
    }
    /**
     * 修改供应商
     * @param req
     * @return
     */
    @RequestMapping("updateSupplier")
    @ResponseBody
    public Map<String, Object> updateSupplier(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String supplierName = req.getParameter("supplierName");
        String linkName = req.getParameter("linkName");
        String linkPhone = req.getParameter("linkPhone");
        String address = req.getParameter("address");
        //获取登录人用户信息
        MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Supplier supplier=supplierService.findSupplierByUserId(ud.getUserId());
        	Map<String, Object> paramMap=new HashMap<String, Object>();
        	paramMap.put("supplierName", supplierName);
        	paramMap.put("supplierId", supplier.getSupplierId());
        	Supplier tempSupplier=supplierService.findSupplierBySupplierName(paramMap);
        	if(tempSupplier!=null){
        		map.put("state", 0);
                map.put("msg", "供货商名称已经存在！");
                return map;
        	}
        	supplier=new Supplier(); 
        	supplier.setUserId(ud.getUserId());
        	supplier.setSupplierName(supplierName);
        	supplier.setLinkName(linkName);
        	supplier.setLinkPhone(linkPhone);
        	supplier.setAddress(address);
            supplierService.updateByPrimaryKeySelective(supplier);
            Users users=new Users();
            users.setUserId(ud.getUserId());
            users.setUserName(linkName);
            users.setRealName(linkName);
            userService.updateByPrimaryKeySelective(users);
            map.put("state", 1);
            map.put("msg", "提交成功");
        } catch (Exception e) {
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "供货商开通失败");
        }
        return map;
    }
    /**
     * 获取供应商
     * @param req
     * @return
     */
    @RequestMapping("getSupplierInfo")
    @ResponseBody
    public Supplier getSupplierInfo(HttpServletRequest req){
        //获取登录人用户信息
        MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Supplier supplier=supplierService.findSupplierByUserId(ud.getUserId());
        	return supplier;
        } catch (Exception e) {
            logger.error(e);
        }
        return null;
    }
    
}
